package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.CryptoUtil;
import com.zhongkai.app.service.AirUserService;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.model.utils.TTHelp;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Chinaset;
import com.zhongkai.model3.SetRole;
import com.zhongkai.model3.SetUnit;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 系统账号管理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AirUserServiceImpl implements AirUserService {
    @Autowired
    private IBaseDao iBaseDao;


    @Autowired
    private PageQuerDao pageQuerDao;


    /**
     * 账号添加
     *
     * @param user_name 账户名
     * @param pass_word 密码
     * @param set_name  姓名
     * @param set_sex   性别
     * @param set_phone 手机号
     * @param set_role  角色id
     */

    @Override
    public ZhongkaiResult addAIR(String user_name, String pass_word, String set_name, String set_sex, String set_phone, int set_role) {
        if (StringUtils.isNullOrEmpty(user_name) || StringUtils.isNullOrEmpty(pass_word)
                || StringUtils.isNullOrEmpty(set_name) || StringUtils.isNullOrEmpty(set_sex)
                || StringUtils.isNullOrEmpty(set_phone) || set_role == 0
                ) {
            return ZhongkaiResult.build(400, "数据不完整,注册失败");
        }


        Chinaset chinaset = (Chinaset) iBaseDao.get("from Chinaset tb where tb.userName='" + user_name + "' or tb.setPhone='" + set_phone + "'");

        if (chinaset != null) {
            return ZhongkaiResult.build(401, "该手机号或用户名已存在");
        }
        chinaset = new Chinaset();

        chinaset.setUuid(TTHelp.generate()); //设置id

        chinaset.setUserName(user_name);

        //加密
        String pass = CryptoUtil.encode64(CryptoUtil.SALT, pass_word);

        chinaset.setPassWord(pass); //设置加密后的密码


        chinaset.setSetName(set_name);

        chinaset.setSetSex(set_sex);
        chinaset.setSetPhone(set_phone);
        chinaset.setSetRole(set_role);
        iBaseDao.save(chinaset);

/*        String generate = TTHelp.generate(); //生成Token
        chinaset.setToken(StringStatic.REDIS_TOKEN + generate); //存入对象*/

/*        ObjectMapper mapper = new ObjectMapper();
        String chinasetJson = null;
        try {
            chinasetJson = mapper.writeValueAsString(chinaset);//转成json
        } catch (IOException e) {
            e.printStackTrace();
            return ZhongkaiResult.build(401, "转换失败,请联系管理员");
        }*/


/*        jedisClient.set(StringStatic.REDIS_TOKEN + generate, chinasetJson);  //加入缓存
        jedisClient.expire(StringStatic.REDIS_TOKEN + generate, 604800);//过期时间为一个星期*/

        return ZhongkaiResult.ok(chinaset);
    }


    /**
     * 账号修改
     *
     * @param uuid      账户uuid
     * @param pass_word 密码
     * @param set_name  姓名
     * @param set_sex   性别
     * @param set_phone 手机号
     * @param set_role  角色id
     */

    @Override
    public ZhongkaiResult updateAIR(String uuid, String pass_word, String set_name, String set_sex, String set_phone, int set_role) {
        if (StringUtils.isNullOrEmpty(pass_word)
                || StringUtils.isNullOrEmpty(set_name) || StringUtils.isNullOrEmpty(set_sex)
                || StringUtils.isNullOrEmpty(set_phone) || set_role == 0
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Chinaset chinaset = (Chinaset) iBaseDao.get("from Chinaset tb where tb.uuid='" + uuid + "'");

        if (chinaset == null) {
            return ZhongkaiResult.build(401, "该用户不存在");
        }


        if (!pass_word.equals(chinaset.getPassWord())) { //如果传入的密码与加密后的不一致则认为密码已被修改
            String pass = CryptoUtil.encode64(CryptoUtil.SALT, pass_word);        //加密


            chinaset.setPassWord(pass); //设置加密后的密码
        }


        chinaset.setSetName(set_name);

        chinaset.setSetSex(set_sex);
        chinaset.setSetPhone(set_phone);
        chinaset.setSetRole(set_role);

        SetUnit setUnit = (SetUnit) iBaseDao.get("from SetUnit su where su.setVolUuid='" + uuid + "'");

        if (setUnit != null){ //如果修改了大客户的你名字 在此处做修改
            setUnit.setSetVolName(set_name);
            iBaseDao.update(setUnit);
        }


        iBaseDao.update(chinaset);

/*        String generate = TTHelp.generate(); //生成Token
        chinaset.setToken(StringStatic.REDIS_TOKEN + generate); //存入对象*/

/*        ObjectMapper mapper = new ObjectMapper();
        String chinasetJson = null;
        try {
            chinasetJson = mapper.writeValueAsString(chinaset);//转成json
        } catch (IOException e) {
            e.printStackTrace();
            return ZhongkaiResult.build(401, "转换失败,请联系管理员");
        }*/


/*        jedisClient.set(StringStatic.REDIS_TOKEN + generate, chinasetJson);  //加入缓存
        jedisClient.expire(StringStatic.REDIS_TOKEN + generate, 604800);//过期时间为一个星期*/

        return ZhongkaiResult.ok();
    }


    /**
     * 账号删除
     *
     * @param uuid 账户uuid
     */


    @Override
    public ZhongkaiResult removeAIR(String uuid) {
        if (StringUtils.isNullOrEmpty(uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Chinaset chinaset = (Chinaset) iBaseDao.get("from Chinaset tb where tb.uuid='" + uuid + "'");

        if (chinaset == null) {
            return ZhongkaiResult.build(401, "该用户不存在");
        }

        iBaseDao.delete(chinaset);

        return ZhongkaiResult.ok();
    }


    /**
     * 账号列表
     *
     * @param page 页码
     * @param rows 条数
     */
    @Override
    public ZhongkaiResult listAIR(Integer page, Integer rows) throws Exception{


        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Chinaset.class);
            pageBean.setDetachedCriteria(detachedCriteria);
/*
            Set<MenusRole> roles = new HashSet<>();
            for (MenusRole menusRole : rows1) {
                roles.
            }*/

            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);

    }


    /**
     * 角色列表
     */
    @Override
    public ZhongkaiResult listAIR_Set() {
        List<SetRole> setRoles = (List<SetRole>) iBaseDao.find("from SetRole");


        return ZhongkaiResult.ok(setRoles);
    }
}
